package com.zt.teacher.controller;


import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.zt.common.Do.ClassWithTeachDO;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.ActiveClass;
import com.zt.common.pojo.common.JsonResult;
import com.zt.common.utils.ClassUtil;
import com.zt.common.utils.JwtUtil;
import com.zt.teacher.dto.ActiveClassDTO;
import com.zt.teacher.dto.ClassTimeDTO;
import com.zt.teacher.dto.DeleteResourcesDTO;
import com.zt.teacher.dto.StuScoreSummaryDTO;
import com.zt.teacher.netty.MyTextWebSocketFrameHandler;
import com.zt.teacher.service.ActiveClassService;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2021-12-16
 */

@RestController
@CrossOrigin
@RequestMapping("activeClass")
@Api("课堂模块")
public class ActiveClassController {

    @Autowired
    private ActiveClassService activeClassService;
    @Autowired
    private MyTextWebSocketFrameHandler handler;

    @PostMapping("insert")
    @ApiOperation("新增课堂")
    public JsonResult addClass(@RequestBody ActiveClassDTO activeClassDTO, HttpServletRequest request){
        activeClassService.insert(activeClassDTO,request);
        return JsonResult.success();
    }

    @GetMapping("getClass/{semester}")
    @ApiOperation("根据老师id获取指定学期该老师的所有课堂")
    public JsonResult<List<ActiveClass>> getClassByTeacherId(@PathVariable String semester, HttpServletRequest request){
        return JsonResult.success(activeClassService.getClassByTeacherId(semester,request));
    }

    @PostMapping("classTime")
    @ApiOperation("根据学期与多个学校班级 获取这些班级当前学期所有必修课上课时间")
    public JsonResult<String> getClassTimeByOriginClassId(@RequestBody ClassTimeDTO classTimeDTO){
        String classTime = activeClassService.getClassTimeByOriginClassId(classTimeDTO);
        return JsonResult.success(classTime);
    }

    @GetMapping("classTime/{semester}")
    @ApiOperation("获取指定老师在指定学期的所有课堂上课时间")
    public JsonResult<String> getClassTimeByTeacherId(@PathVariable String semester,HttpServletRequest request){
        String classTime = activeClassService.getClassTimeByTeacherId(semester,request);
        return JsonResult.success(classTime);
    }

    @PostMapping("attend/{classId}")
    @ApiOperation("开启上课")
    public JsonResult attendClass(@PathVariable int classId, HttpServletRequest request) throws Exception {
        activeClassService.attendClass(classId,request);
        return JsonResult.success();
    }

    @PostMapping("quit/{id}")
    @ApiOperation("结束上课")
    public JsonResult quitClass(@PathVariable Integer id) throws Exception {
        handler.removeClass(id);
        return JsonResult.success();
    }

    @GetMapping("getTeacherCurrentClass/{semester}")
    @ApiOperation("获取老师正在上课的课堂")
    public JsonResult<List<ActiveClass>> getTeacherCurrentClass(@PathVariable String semester, HttpServletRequest request){
        List<ActiveClass> classes = activeClassService.getTeacherCurrentClass(semester, request);
        return JsonResult.success(classes);
    }

    @GetMapping("getStudentCurrentClass/{semester}")
    @ApiOperation("由学生端请求 获取学生正在上课的课堂")
    public JsonResult<List<ClassWithTeachDO>> getStudentCurrentClass(@PathVariable String semester, HttpServletRequest request){
        List<ClassWithTeachDO> classes = activeClassService.getStudentCurrentClass(semester, request);
        return JsonResult.success(classes);
    }

    @PostMapping("upload/{classId}")
    @ApiOperation("向指定班级提交班级资源")
    public JsonResult<String> upload(MultipartFile file,@PathVariable int classId) throws IOException {
        String fileName = activeClassService.uploadResources(file, classId);
        return JsonResult.success(fileName);
    }

    @GetMapping("getResources/{classId}")
    @ApiOperation("根据班级id获取该班级所有资源名字")
    public JsonResult<String[]> getResources(@PathVariable int classId){
        try {
            String[] resources = ClassUtil.getClassResources(classId);
            return JsonResult.success(resources);
        }catch (NullPointerException e){
            throw new ApiException(CodeMsg.CLASS_NOT_EXISTED);
        }
    }

    @DeleteMapping("deleteResources")
    @ApiOperation("删除资源")
    public JsonResult deleteResources(@RequestBody DeleteResourcesDTO deleteResourcesDTO){
        activeClassService.deleteResources(deleteResourcesDTO.getClassId(),deleteResourcesDTO.getFileName());
        return JsonResult.success();
    }

    @GetMapping("getAllSummary/{classId}")
    @ApiOperation("获取该班级所有学生所有考试、作业得分情况以及签到次数")
    public JsonResult<List<StuScoreSummaryDTO>> getSummaryByClassId(HttpServletRequest request, @PathVariable int classId){
        return JsonResult.success(activeClassService.getSummaryByClassId(request,classId));
    }
}
